package com.lovo.project.servlet;

import com.lovo.project.model.SalesVolumeDO;
import com.lovo.project.service.SalesVolumeService;
import com.lovo.project.service.impl.SalesVolumeServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "ShowSalesVolumeServlet",value = "/showSalesVolume")
public class ShowSalesVolumeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SalesVolumeService salesVolumeService = new SalesVolumeServiceImpl();
        List<SalesVolumeDO> list = new ArrayList<>();
        List<Double> moneyList = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        String item = request.getParameter("item");
        if("year".equals(item)){
            list = salesVolumeService.findByYear();
        }else{
            list = salesVolumeService.findByMonth();
        }
        for (SalesVolumeDO salesVolumeDO : list) {
            moneyList.add(salesVolumeDO.getMoney());
            timeList.add(salesVolumeDO.getSaleDate());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("time",timeList);
        map.put("money",moneyList);
        response.getWriter().println(ResultVO.success().add(map).toJson());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
